package com.hgk.wanwan.main.fragment.persentfragment.util;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by lenovo on 2016/10/21.
 */
public class PaintOrignalView extends View {
    private Paint mPaint = new Paint();
    private Path mPath = new Path();
    private final RectF mDirtyRect = new RectF();
    private static final float STROKE_WIDTH = 5f;
    private static final float HALF_STROKE_WIDTH = STROKE_WIDTH/2;
    private float mLastTouchX = 0;
    private float mLastTouchY = 0;

    public PaintOrignalView(Context context) {
        super(context);
        initPaintView();
    }

    public PaintOrignalView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaintView();
    }

    public PaintOrignalView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaintView();
    }

    public void clear(){
        if(mPath!=null){
            mPath.reset();
            invalidate();
        }
    }
    private void initPaintView() {
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeWidth(10f);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN: {
                mPath.moveTo(eventX, eventY);
                mLastTouchX = eventX;
                mLastTouchY = eventY;
                invalidate();
            }
            return true;
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:{
                resstDirRect(eventX, eventY);
                int historySize = event.getHistorySize();
                for (int i = 0; i < historySize; i++) {
                    float historicalX = event.getHistoricalX(i);
                    float historicalY = event.getHistoricalY(i);
                    getDirtyRect(historicalX,historicalY);
                    mPath.lineTo(historicalX, historicalY);
                }
                mPath.moveTo(eventX, eventY);
                invalidate((int) (mDirtyRect.left - HALF_STROKE_WIDTH),
                        (int) (mDirtyRect.top - HALF_STROKE_WIDTH),
                        (int) (mDirtyRect.right - HALF_STROKE_WIDTH),
                        (int) (mDirtyRect.bottom - HALF_STROKE_WIDTH));
                mLastTouchY = eventY;
                mLastTouchX = eventX;
            }
            break;
            default:{

            }
                return false;
        }
        return super.onTouchEvent(event);
    }

    private void getDirtyRect(float historicalX, float historicalY) {
        if (historicalX < mDirtyRect.left) {
            mDirtyRect.left = historicalX;
        } else if (historicalX > mDirtyRect.right) {
            mDirtyRect.right = historicalX;
        }
        if (historicalY < mDirtyRect.top) {
            mDirtyRect.top = historicalY;
        } else if (historicalY > mDirtyRect.bottom) {
            mDirtyRect.bottom = historicalY;
        }
    }

    private void resstDirRect(float eventX, float eventY) {
        mDirtyRect.left = Math.min(mLastTouchX, eventX);
        mDirtyRect.right = Math.max(mLastTouchX, eventX);
        mDirtyRect.top = Math.min(mLastTouchY, eventY);
        mDirtyRect.bottom = Math.max(mLastTouchY, eventY);
    }
}
